package com.example.wl.tiaoyinqi;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by wl on 2016/5/26.
 */
public class QinxianView extends ImageView {

    private int xianColor;
    private Paint mPaintXian, mPaintQinkou;
    private int qinkouRadius, qinxianNum;
    private int selectedXianNum;
    private int[] x0, y0, x1, y1, x2, y2, x3, y3;
    private int[] leftMargin, leftTopMargin, rightTopMargin, qinzhenLeftMargin;
    private onXianClickListener mListener;
    private boolean hasQinkou;
    private boolean hasQinzhen;
    private Drawable qinzhen;

    public interface onXianClickListener {
        void xianClick(int position);
    }

    public QinxianView(Context context) {
        this(context, null);
    }

    public QinxianView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QinxianView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.QinxianView, defStyleAttr, 0);
        for (int i = 0; i < a.getIndexCount(); i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.QinxianView_xianColor:
                    xianColor = a.getColor(attr, 0x977c69);
                    break;
                case R.styleable.QinxianView_hasQinkou:
                    hasQinkou = a.getBoolean(attr, true);
                    break;
                case R.styleable.QinxianView_hasQinzhen:
                    hasQinzhen = a.getBoolean(attr, false);
            }
        }
        a.recycle();

        qinkouRadius = hasQinkou ? dip2px(9) : 0;
        qinzhen = getResources().getDrawable(R.drawable.pic_qinzhen);
        selectedXianNum = -1;

        mPaintXian = new Paint();
        mPaintXian.setAntiAlias(true);
        mPaintXian.setStrokeWidth(dip2px(3));
        mPaintXian.setColor(xianColor);
        mPaintQinkou = new Paint();
        mPaintQinkou.setAntiAlias(true);
        mPaintQinkou.setStrokeWidth(dip2px(2));
        mPaintQinkou.setStyle(Paint.Style.STROKE);
        mPaintQinkou.setColor(Color.WHITE);

        leftMargin = new int[]{
                187, 126, 78, 46, 27,
                17, 18, 29, 49, 77,
                112, 160, 225, 309, 410
        };

        leftTopMargin = new int[]{
                333, 427, 522, 617, 713,
                806, 902, 998, 1092, 1188,
                1283, 1377, 1472, 1567, 1663
        };
        rightTopMargin = new int[]{
                455, 549, 628, 717, 787,
                853, 928, 1004, 1070, 1147,
                1231, 1325, 1413, 1520, 1627
        };
        qinzhenLeftMargin = new int[leftTopMargin.length];
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        qinxianNum = leftTopMargin.length;
        x0 = new int[qinxianNum];
        x1 = new int[qinxianNum];
        x2 = new int[qinxianNum];
        x3 = new int[qinxianNum];
        y0 = new int[qinxianNum];
        y1 = new int[qinxianNum];
        y2 = new int[qinxianNum];
        y3 = new int[qinxianNum];
        for (int i = 0; i < qinxianNum; i++) {
            x0[i] = dip2px(leftMargin[i]) / 2 + qinkouRadius;
            x1[i] = x0[i] + qinkouRadius;
            x2[i] = widthSize;
            x3[i] = dip2px(qinzhenLeftMargin[i]) / 2;

            y0[i] = dip2px(leftTopMargin[i] - 299) / 2 + qinkouRadius;
            y1[i] = y0[i];
            y2[i] = dip2px(rightTopMargin[i] - 299) / 2;
            y3[i] = y2[i];
        }

        int heightSize = y1[qinxianNum - 1] > y2[qinxianNum - 1] ?
                y1[qinxianNum - 1] + dip2px(30) :
                y1[qinxianNum - 1] + dip2px(30);

        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < qinxianNum; i++) {
            if (i == selectedXianNum) {
                mPaintXian.setColor(getResources().getColor(R.color.colorXianSelected));
                canvas.drawLine(x1[i], y1[i], x2[i], y2[i], mPaintXian);
                mPaintXian.setColor(xianColor);
            } else {
                canvas.drawLine(x1[i], y1[i], x2[i], y2[i], mPaintXian);
            }
            if (hasQinzhen) {
                qinzhen.setBounds(x3[i], y3[i] - qinzhen.getIntrinsicHeight() / 2,
                        x3[i] + qinzhen.getIntrinsicWidth(), y3[i] + qinzhen.getIntrinsicHeight() / 2);
                qinzhen.draw(canvas);
            }
            canvas.drawCircle(x0[i], y0[i], qinkouRadius, mPaintQinkou);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            float x = event.getX();
            float y = event.getY();
            if (y < (getLimitY(x, 0) + getLimitY(x, 1)) / 2) {
                selectedXianNum = 0;
            } else if (y > (getLimitY(x, qinxianNum - 1) + getLimitY(x, qinxianNum - 2)) / 2) {
                selectedXianNum = qinxianNum - 1;
            } else {
                for (int i = 1; i < qinxianNum - 1; i++) {
                    float j = getLimitY(x, i);
                    float k = getLimitY(x, i + 1);
                    float p = getLimitY(x, i - 1);
                    if (y < (j + k) / 2 &&
                            y > (j + p) / 2) {
                        selectedXianNum = i;
                        break;
                    }
                }
            }
            mListener.xianClick(selectedXianNum);
            invalidate();
        }
        return true;
    }

    public void setLeftMargin(int[] leftMargin) {
        this.leftMargin = leftMargin;
    }

    public void setLeftTopMargin(int[] leftTopMargin) {
        this.leftTopMargin = leftTopMargin;
    }

    public void setRightTopMargin(int[] rightTopMargin) {
        this.rightTopMargin = rightTopMargin;
    }

    public void setQinzhenLeftMargin(int[] qinzhenLeftMargin) {
        this.qinzhenLeftMargin = qinzhenLeftMargin;
    }

    private int dip2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    private float getLimitY(float x, int i) {
        return (x - x1[i]) * (y2[i] - y1[i]) / (x2[i] - x1[i]) + y1[i];
    }

    public void setOnXianClickListener(onXianClickListener listener) {
        mListener = listener;
    }
}
